package com.example.simpleadapter;

import static android.widget.AdapterView.*;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private final String[] names =new String[]{"Lion","Tiger","Monkey","Dog","Cat"};
    private final int[] imageIds = new int[]{R.drawable.lion,R.drawable.tiger,R.drawable.monkey,R.drawable.dog,
    R.drawable.cat};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建一个list集合，List集合的元素是Map
        List<Map<String,Object>> listItems = new ArrayList<>();
        for (int i =0;i < names.length;i++)
        {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("animalName",names[i]);
            listItem.put("animalImage",imageIds[i]);
            listItems.add(listItem);
        }
        //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter =new SimpleAdapter(this, listItems,R.layout.layout,
                new String[]{"animalName","animalImage"},new int[]{R.id.name,R.id.image});
        ListView list = (ListView) findViewById(R.id.mylist);

        //为ListView设置Adapter
        list.setAdapter(simpleAdapter);
        list.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
            {
                switch (position){
                    case 0:

                        Toast.makeText(MainActivity.this, "Lion", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:

                        Toast.makeText(MainActivity.this, "Tiger", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:

                        Toast.makeText(MainActivity.this, "Monkey", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:

                        Toast.makeText(MainActivity.this, "Dog", Toast.LENGTH_SHORT).show();
                        break;
                    case 4:

                        Toast.makeText(MainActivity.this, "Cat", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        });


    }
}